package com.service.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.common.wx.TokenSingleton;
import com.common.wx.WeChatData;
import com.common.wx.WebUtil;
import com.common.wx.WechatUtil;

@Service("wxService")
public class WXService {

	static final Logger logger = LoggerFactory.getLogger(WXService.class);
	@Value("${qywx.AGENTID}")
	public String agentIdString;
	@Value("${qywx.appSecret}")
	public String secretString;

	public void sendTxtMessage(String users, String content) throws IOException {
		WeChatData wcd = new WeChatData();
		WechatUtil.AGENTID = Integer.parseInt(agentIdString);
		WechatUtil.APPSECRET = secretString;
		wcd.setTouser(users);
		wcd.setAgentid(WechatUtil.AGENTID);
		wcd.setMsgtype("text");
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("content", content);
		wcd.setText(map);
		String dataJson = JSONObject.toJSONString(wcd);
		System.out.println("postdata:" + dataJson);

		String accessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";

		TokenSingleton tokenSingleton = TokenSingleton.getInstance();
		Map<String, String> mapT = tokenSingleton.getMap();
		String requestUrl = String.format(accessTokenUrl, mapT.get("access_token"));

		String CONTENT_TYPE = "Content-Type";
		String content_type = "Content-Type";
		String result = WebUtil.post("utf-8", CONTENT_TYPE, content_type, requestUrl, dataJson);
		System.out.println("result:" + result);
	}

	public String getUserId(String code) {
		String userId = "";
		WechatUtil.AGENTID = Integer.parseInt(agentIdString);
		WechatUtil.APPSECRET = secretString;
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s";
		TokenSingleton tokenSingleton = TokenSingleton.getInstance();
		Map<String, String> mapT = tokenSingleton.getMap();
		String requestUrl = String.format(url, mapT.get("access_token"), code);
		JSONObject result = WebUtil.doGet(requestUrl);
		System.out.println("getUserId:" + result);
		userId = result.getString("UserId");
		if (userId == null) {
			userId = "";
		}
		System.out.println("userId:" + userId);
		return userId;
	}

	public String getImgSrc(String userId) {
		String imgsrc = "";
		WechatUtil.AGENTID = Integer.parseInt(agentIdString);
		WechatUtil.APPSECRET = secretString;
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s";
		TokenSingleton tokenSingleton = TokenSingleton.getInstance();
		Map<String, String> mapT = tokenSingleton.getMap();
		String requestUrl = String.format(url, mapT.get("access_token"), userId);
		JSONObject result = WebUtil.doGet(requestUrl);
		System.out.println("getImgSrc:" + result);
		imgsrc = result.getString("avatar");
		System.out.println("imgsrc:" + imgsrc);
		return imgsrc;
	}
}
